import jieba
from matplotlib import pylab as plt
from wordcloud import WordCloud
import numpy as np
from PIL import Image
from pymysql import *
import json

def get_img(field,targetImageSrc,resImageSrc):
    con = connect(host='localhost', user='root', passwd='admin', db='carsell',port=3306,charset='utf8mb4')
    cusor = con.cursor()
    sql = f"select {field} from carinfo"
    cusor.execute(sql)
    data = cusor.fetchall()

    text = ''  #查询到的词存到text变量
    for i in data:
        if i[0] != '' :
            targArr = i
            for j in targArr:
                text += j
    cusor.close()
    con.close()
    data_cut = jieba.cut(text, cut_all=False)
    string = ''.join(data_cut)

    print(text)

    ##图片（根据词组
    img = Image.open(targetImageSrc)
    img_arr = np.array(img)
    wd = WordCloud(
        font_path='STHUPO.TTF',
        mask=img_arr,
        background_color='#04122c'
    )
    wd.generate_from_text(string)
    ##绘制图片 plt
    fig = plt.figure(1)
    plt.imshow(wd)
    plt.axis('off')

    plt.savefig(resImageSrc,dpi=800)

get_img('manufacture','./big-screen/public/R-C.jpg','./big-screen/public/R-Cloud.png')